public class LeetCode1071 {

    public static void main(String[] args) {
        LeetCode1071 leetCode1071 = new LeetCode1071();
        System.out.println(leetCode1071.gcdOfStrings1("acb", "abc"));;

    }

    public String gcdOfStrings1(String str1, String str2) {
            int s1Length = str1.length();
            int s2Length = str2.length();
            int yushu = s1Length % s2Length;
            //最大公约数结果
            int lastBeiChuShu = s2Length;

            int result = yushu == 0 ? s2Length : 0;

            // 得到最大公约数
            while (yushu != 0){
                int tempBeiChuShu = yushu;
                yushu = lastBeiChuShu % tempBeiChuShu;
                lastBeiChuShu = tempBeiChuShu;
            }
            result = lastBeiChuShu;

            if (check(str1, str2, result) && check(str2, str2, result)){
                return str1.substring(0, result);
            }

        return "";
    }

    public boolean check(String data,String checkData, int subLength){
        String sub = checkData.substring(0, subLength);
        int i = 1;
        String apendData = sub;
        while (data.contains(apendData)){
            i++;
            if (data.length() > apendData.length()){
                apendData += sub;
            }else {
                break;
            }
        }
        if (apendData.equals(data)){
            return true;
        }

        return false;
    }
}
